import {Injectable} from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class ProductService {

  private products: Array<Product> = [
    new Product(1, '第一个商品', 1.86, 4.5, '这是第一个商品', ['手机']),
    new Product(2, '第二个商品', 3.6, 5, '这是第二个商品', ['珠宝', '奢侈品']),
    new Product(3, '第三个商品', 5.06, 4.5, '这是第三个商品', ['手机']),
    new Product(4, '第四个商品', 4.26, 3.5, '这是第四个商品', ['手机', '终端']),
    new Product(5, '第五个商品', 12.16, 4, '这是我的第五个商品', ['MP4', '电子设备']),
    new Product(6, '第六个商品', 11.4, 2.5, '这是我的第六个商品', ['图示']),
    new Product(7, '第七个商品', 121.4, 3.5, '这是我的第七个商品', ['图示'])
  ];

  private comments: Array<Comment> = [
    new Comment(1, 1, '2019-05-26 10:34:05', 4.5, '这东西还行', '张三'),
    new Comment(2, 1, '2019-01-02 13:24:15', 4.5, '这东西还行，这是我的第一个商品', '**sk'),
    new Comment(3, 3, '2019-02-26 17:32:35', 4.5, '这东西不好，这是我的第一个商品', '李四'),
    new Comment(4, 4, '2018-05-16 11:54:20', 3.5, '这东西还行这是我的第一个商品', '王五'),
    new Comment(5, 4, '2016-02-26 22:14:25', 4, '这东西还行这是我的第一个商品', '张三'),
    new Comment(6, 6, '2018-03-16 20:33:52', 2.5, '这东西一般般', '赵六')
  ];

  constructor() {
  }

  getProducts() {
    return this.products;
  }

  getProduct(id: number) {
    return this.products.find((p) => p.id == id);
  }

  getCommentsForProduct(id: number): Comment[] {
    return this.comments.filter((comment: Comment) => comment.productId == id);
  }
}

export class Product {

  constructor(
    public id: number,
    public title: string,
    public price: number,
    public rating: number,
    public desc: string,
    public categories: Array<string>
  ) {
  }

}

export class Comment {

  constructor(
    public id: number,
    public productId: number,
    public time: string,
    public rating: number,
    public content: string,
    public user: string
  ) {
  }

}
